<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Hooks.php
 * Date: 2019/2/14
 * Time: 14:41
 * Description: 钩子列表
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\Hooks as HooksModel;
use think\facade\Request;

class Hooks extends Admin
{
    /**
     * 插件列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if (Request::isAjax()) {
            $hooksModel           = new HooksModel();
            return $hooksModel->tableData(input('param.'));
        }
        return $this->fetch('index');
    }

    /**
     * 钩子添加
     * @return array|mixed
     */
    public function add()
    {
        $this->view->engine->layout(false);
        if(Request::isPost())
        {
            $hooksModel = new HooksModel();
            return $hooksModel->addData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     * 钩子修改
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $this->view->engine->layout(false);
        $hooksModel = new HooksModel();
        if(Request::isPost())
        {
            return $hooksModel->saveData(input('param.'));
        }
        $data = $hooksModel->where('id',input('param.id/d'))->find();
        if (!$data) {
            return error_code(10002);
        }
        return $this->fetch('edit',['data' => $data]);
    }

    /**
     * 钩子软删除
     * @return array
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg' => '删除失败',
            'data' => []
        ];
        $hooksModel = new HooksModel();
        if ($hooksModel::destroy(input('param.id/d'))) {
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }
        return $result;
    }
}